//
//  ABSpringLoadingView.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-28.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ABSpringLoadingView.h"
#import <QuartzCore/QuartzCore.h>

@implementation ABSpringLoadingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        UIView *blackBackView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
        blackBackView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        blackBackView.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.5);
        blackBackView.layer.shadowColor = [UIColor blackColor].CGColor;
        blackBackView.layer.shadowOpacity = 0.45;
        blackBackView.layer.shadowOffset = CGSizeMake(0, 0);
        blackBackView.layer.masksToBounds = NO;
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:blackBackView.bounds];
        blackBackView.layer.shadowPath = path.CGPath;
        [self addSubview:blackBackView];
        
        // Like the one in the Letterpress app by Loren Brichter (atebits.com)
        spinnerView = [[[TISpringLoadedSpinnerView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)] autorelease];
        [blackBackView addSubview:spinnerView];
        
        // Close button
//        UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        closeButton.frame = CGRectMake(44, 0, 44, 44);
//        [closeButton setTitle:@"X" forState:UIControlStateNormal];
//        [closeButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
//        [blackBackView addSubview:closeButton];
        
        // Create the display link. I use one to handle all the views.
        displayLink = [[CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)] retain];
        [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:(id)kCFRunLoopCommonModes];
    }
    return self;
}

- (void)displayLinkTick:(CADisplayLink *)link {
	[spinnerView simulateSpringWithDisplayLink:link];
}

- (void)close
{
    if ([_delegate respondsToSelector:@selector(userClose)]) {
        [_delegate performSelector:@selector(userClose)];
    }
}

- (void)dealloc {
	[displayLink invalidate];
	[displayLink release];
	[super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
